{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="{% static 'finance/js/jquery-1.8.3.min.js' %}"></script>
    <link href="{% static 'finance/css/bootstrap.min.css' %}" rel="stylesheet">
    <link href="{% static 'finance/css/bootstrap-datetimepicker.css' %}" rel="stylesheet">
    <script src="{% static 'finance/js/bootstrap.min.js' %}"></script>
    <script src="{% static 'finance/js/bootstrap-datetimepicker.min.js' %}"></script>
    <script type="text/javascript" src="{% static 'finance/js/bootstrap-datetimepicker.zh-CN.js' %}" charset="UTF-8"></script>

</head>
<body>
<div class="container-fluid">
	<div class="row-fluid">
		<div class="span12">
			<h3 class="text-center">
				新浪财经要闻
			</h3>
			<h3 class="text-left">
				收集当天的数据
			</h3>
			<div class="alert" id="tip">
				 <button type="button" class="close" data-dismiss="alert">×</button>
				<h4 id="title">
					提示!
				</h4>
                <p id="tip_body">请注意你的个人隐私安全.</p>
			</div>
            <label class="control-label">是否开始爬虫</label>
            <button class="btn" type="button" onclick="start(this)">开始</button>
            <div class="control-group">
                <label class="control-label">选择查看某天的数据</label>
                <div class="controls input-append date form_date" data-date="" data-date-format="dd MM yyyy" data-link-field="dtp_input2" data-link-format="yyyy-mm-dd">
                    <input size="16" type="text" value="" readonly>
                    <span class="add-on"><i class="icon-remove"></i></span>
					<span class="add-on"><i class="icon-th"></i></span>
                </div>
				<input type="hidden" id="dtp_input2" value="" /><br/>
            </div>
			<table class="table">
				<thead>
					<tr>
						<th>
							编号
						</th>
						<th>
							标题
						</th>
						<th>
							链接
						</th>
						<th>
							爬虫jod_id
						</th>
                        <th>
                            关键字
                        </th>
                        <th>
                            时间
                        </th>
					</tr>
				</thead>
				<tbody id="content" hav="1">
{#					<tr>#}
{#						<td>#}
{#							1#}
{#						</td>#}
{#						<td>#}
{#							TB - Monthly#}
{#						</td>#}
{#						<td>#}
{#							01/04/2012#}
{#						</td>#}
{#						<td>#}
{#							Default#}
{#						</td>#}
{#					</tr>#}

				</tbody>
			</table>
		</div>
	</div>
</div>
<input id="jod_id" type="hidden">
            {% csrf_token %}
</body>
<script>
    $(".alert").hide();
    $('.form_date').datetimepicker({
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0
    });



    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }

    function tip(state,title,body) {
        if(state==0){
            $('#tip').attr('class','alert');
        }
        else if(state==1){
            $('#tip').attr('class','alert alert-success');
        }
        else{
            $('#tip').attr('class','alert alert-error');
        }

        $('#title').text(title);
        $('#tip_body').text(body);
        $(".alert").show();
    }



    var jod_id='';
    var jod_obj=$('#jod_id');
    var csrfvaule=getCookie('csrftoken');
    var start_count=0;
    var end_cont=10;
    var start_timeout=0;
    function start(th){
        $.ajax({
            url: '{% url 'start_spider' %}',
            type: 'POST',
            async: false,
            dataType: 'json',
            data: {"csrfmiddlewaretoken":csrfvaule}
        })
        .done(function(data) {
            var is_tip=data['is_tip'];
            var is_success=data['is_success'];
            var msg=data['msg'];
            if(is_success==1) {
                jod_id = data['data']['jod_id']
                jod_obj.val(jod_id);
                tip(1, '状态提示', msg);
                start_timeout=1;
            }
            else{
                    tip(2,'状态提示',msg)
                }
            start_count=0;

        })
        .fail(function() {
            tip(2,'状态提示','连接服务错误')
        });

        if(start_timeout==1){
           setInterval(get_jod_item,1000)
        }

    }


    function get_jod_item(){
            jod_id=jod_obj.val();
            if(jod_id=='' || jod_id==null){
                return
            }

            if(start_count>end_cont){
                return
            }
            else if(start_count==end_cont){
                    tip(2, '获取jod数据', '获取数据失败，当前爬虫任务暂无数据');
                start_count=start_count+1;
                }
                else{
                start_count=start_count+1;
                $.ajax({
                    url: '{% url 'get_itmes' %}',
                    type: 'POST',
                    dataType: 'json',
                    data: {"csrfmiddlewaretoken":csrfvaule,"jod_id":jod_id}
                })
                .done(function(data) {
                    var is_tip=data['is_tip'];
                    var is_success=data['is_success'];
                    var msg=data['msg'];
                    var data=data['data']['str_list'];
                    $('#content').html('');
                    if(is_success==1){
                        if(data.length>0){
                           for(i=0;i<data.length;++i) {
                            $('#content').append('<tr><td>' + i + '</td><td>'+data[i]['title']+'</td><td>'+data[i]['href']+'</td><td>'+data[i]['jod_id']+'</td><td>'+data[i]['word_list']+'</td><td>'+data[i]['time']+'</td></tr>')
                        }
                        start_count=101;
                        tip(1, '获取jod数据', msg);
                        }
                        else{
                            tip(1, '获取jod数据', '等待数据中。。。。。。。');
                        }

                    }
                })
                .fail(function() {
                    tip(2,'获取jod数据','连接服务错误')
                });
                }

            }



    function objgetTime(){  //需重新计算时间的格式  2016-1-11 14:31:10
        var objtimestarset="";
        var today=new Date();
        var ymd = today.toLocaleDateString().split("/");
        var y=ymd[0];
        var mon=ymd[1];
        var d=ymd[2];
        var h=today.getHours();
        var m=today.getMinutes();
        var s=today.getSeconds();
        var ms =today.getMilliseconds()
        // add a zero in front of numbers<10
        m=checkTime(m)
        s=checkTime(s)
        objtimestarset=y+"-"+mon+"-"+d+" "+h+":"+m+":"+s+"."+ms;
        return objtimestarset
    }
    function checkTime(i){//0-9 补位
        if (i<10)
          {i="0" + i}
          return i
    }


    $(document).ready(
        )



    $('.form_date')
    .datetimepicker()
    .on('changeDate', function(ev){
        var odate=ev.date.getFullYear().toString() + "-"+ (ev.date.getMonth()+1).toString()+ "-"+ ev.date.getDate().toString()
        $.ajax({
            url: '{% url 'get_day_items' %}',
            type: 'POST',
            dataType: 'json',
            data: {"csrfmiddlewaretoken":csrfvaule,"date":odate}
        })
        .done(function(data) {
            var is_tip=data['is_tip'];
            var is_success=data['is_success'];
            var msg=data['msg'];
            var data=data['data']['str_list'];
            $('#content').html('');
            if(is_success==1){
                if(data.length>0){
                   for(i=0;i<data.length;++i) {
                    $('#content').append('<tr><td>' + i + '</td><td>'+data[i]['title']+'</td><td>'+data[i]['href']+'</td><td>'+data[i]['jod_id']+'</td><td>'+data[i]['word_list']+'</td><td>'+data[i]['time']+'</td></tr>')
                }
                start_count=11;
                tip(1, '获取当天数据', msg);
                }
                else{
                    tip(0, '获取当天数据', '当天暂无数据');
                }

            }
        })
        .fail(function() {
            tip(2,'获取当天数据','连接服务错误')
        });
    });
</script>
</html>